package com.tm.commonutils.utils;

import java.util.Random;

public class ValidateCodeUtils {
 
    /**
     * 随机生成验证码
     *
     * @param
     * @return
     */
    public static String generateValidateCode6String() {
        return String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, 5)));
    }
 
    /**
     * 随机生成验证码
     *
     * @param length 长度为4位或者6位
     * @return
     */
    public static String generateValidateCode(int length) {
        String code = null;
        if (length == 4) {
            code = String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, 3)));
        } else if (length == 6) {
            code = String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, 5)));
        } else {
            throw new RuntimeException("只能生成4位或6位数字验证码");
        }
        return code;
    }
    public static String generateValidateCode() {
        return generateValidateCode(6);
    }

    /**
     * 随机生成指定长度字符串验证码
     *
     * @param length 长度
     * @return
     */
    public static String generateValidateCode4String(int length) {
        Random rdm = new Random();
        String hash1 = Integer.toHexString(rdm.nextInt());
        String capstr = hash1.substring(0, length);
        return capstr;
    }
 
 
    /*
     * @description:获取字母
     * @author: yan
     * @date: 2023/3/2 9:51
     * @param length 长度
     * @return: java.lang.String
     **/
    public static String generateLetter(int length) {
        if (length <= 0) {
            throw new RuntimeException("Illegal value");
        }
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (; ; ) {
            int num = random.nextInt(58) + 65;
            if (num >= 65 && num <= 90) {
                sb.append((char) (num));
            } else if (num >= 97 && num <= 122) {
                sb.append((char) (num));
            }
            if (sb.toString().length() > length - 1) {
                break;
            }
        }
        return sb.toString();
    }
 
    /*
     * @description:产生大小写数字随机验证码
     * @author: yan
     * @date: 2023/3/2 10:26
     * @param length
     * @return: java.lang.String
     **/
    public static String generateNumAndLetter(int length) {
        if (length <= 0) {
            throw new RuntimeException("Illegal value");
        }
        StringBuffer sb = new StringBuffer();
        for (; ; ) {
            double rand = Math.random();
            double randTri = Math.random() * 3;
            if (randTri >= 0 && randTri < 1) {
                sb.append((char) (rand * ('9' - '0') + '0'));
            } else if (randTri >= 1 && randTri < 2) {
                sb.append((char) (rand * ('Z' - 'A') + 'A'));
            } else {
                sb.append((char) (rand * ('z' - 'a') + 'a'));
            }
            if (sb.toString().length() > length - 1) {
                break;
            }
        }
        return sb.toString();
    }
 
 
    public static void main(String[] args) {
        System.out.println(generateNumAndLetter(5));
    }
 
 
}